package tables

import (
	// cherryeditor "adm1/components/cherry_editor"
	// wangeditor "adm1/components/wang_editor"
	"fmt"
	template2 "html/template"

	"github.com/GoAdminGroup/go-admin/context"
	"github.com/GoAdminGroup/go-admin/modules/db"
	"github.com/GoAdminGroup/go-admin/plugins/admin/modules/table"
	"github.com/GoAdminGroup/go-admin/template"
	"github.com/GoAdminGroup/go-admin/template/types"
	"github.com/GoAdminGroup/go-admin/template/types/action"
	"github.com/GoAdminGroup/go-admin/template/types/form"
	editType "github.com/GoAdminGroup/go-admin/template/types/table"
	_ "modernc.org/sqlite"
)

func GetAycontentTable(ctx *context.Context) table.Table {

	ayContent := table.NewDefaultTable(ctx,
		table.DefaultConfigWithDriverAndConnection("sqlite", "pbootcms"))

	info := ayContent.GetInfo().SetFilterFormLayout(form.LayoutThreeCol)
	tblName := "ay_content"

	// 分类编码和分类名称映射
	scodes, _ := db.WithDriverAndConnection("pbootcms", conn).
		Table("ay_content_sort").Select("scode", "name").All()
	fopts := make([]types.FieldOption, len(scodes))
	for i, v := range scodes {
		// fmt.Printf("%+v", v)
		// println()
		// println()
		fopts[i].Text = fmt.Sprint(v["name"])
		fopts[i].Value = fmt.Sprint(v["scode"])
	}
	// slices.SortFunc(fopts, func(a, b types.FieldOption) int {
	// 	if a.Value < b.Value {
	// 		return -1
	// 	} else {
	// 		return 1
	// 	}
	// })

	// 统计各分类下有多少文章
	scode := "scode"
	results, _ := db.WithDriverAndConnection("pbootcms", conn).
		Table(tblName).Select(scode, "count(scode)").GroupBy(scode).All()
	heads := make(types.Thead, len(results))
	infoItems := make(map[string]types.InfoItem, len(results))
	for i, v := range results {
		// fmt.Printf("%+v", v)
		// println()
		for _, v1 := range scodes {
			if v[scode] == v1[scode] {
				name := fmt.Sprint(v1["name"])
				v1 := fmt.Sprint(v["count(`scode`)"])
				heads[i] = types.TheadItem{Head: name}
				infoItems[name] = types.InfoItem{Content: template2.HTML(v1)}
			}
		}
	}
	info.SetWrapper(func(content template2.HTML) template2.HTML {
		tableContent := template.Default().Table().SetThead(heads).
			SetInfoList([]map[string]types.InfoItem{infoItems}).GetContent()
		return template.Default().Box().
			SetBody(tableContent).
			SetNoPadding().
			WithHeadBorder().
			GetContent() + content
	})

	info.AddField("编号", "id", db.Integer).FieldSortable()
	info.AddField("sname", "name", db.Varchar).FieldJoin(types.Join{
		Field:     "scode",
		JoinField: "scode",
		Table:     "ay_content_sort",
	}).FieldHide()

	info.AddSelectBox(ctx, "栏目", fopts, action.FieldFilter("scode"))

	info.AddField("标题", "title", db.Text).
		FieldFilterable(types.FilterType{Operator: types.FilterOperatorLike}).
		FieldSortable().FieldEditAble()
	info.AddField("标题颜色", "titlecolor", db.Text).FieldHide()
	info.AddField("副标题", "subtitle", db.Text).FieldHide()
	info.AddField("自定义文件名", "filename", db.Text).FieldHide()
	info.AddField("作者", "author", db.Text).FieldHide()
	info.AddField("来源", "source", db.Text).FieldHide()
	info.AddField("'外链地址", "outlink", db.Text).FieldHide()
	info.AddField("发布日期", "date", db.Text).FieldHide().FieldFilterable().FieldSortable()
	info.AddField("缩略图", "ico", db.Text).FieldHide()
	info.AddField("多图片", "pics", db.Text).FieldHide()
	info.AddField("内容", "content", db.Text).FieldHide().FieldSortable().
		FieldFilterable(types.FilterType{FormType: form.Text, Operator: types.FilterOperatorLike})

	info.AddField("tag关键字", "tags", db.Text).FieldHide().FieldEditAble()
	info.AddField("附件", "enclosure", db.Text).FieldHide()
	info.AddField("关键字", "keywords", db.Text).FieldHide()
	info.AddField("描述", "description", db.Text).FieldFilterable().
		FieldEditAble(editType.Textarea).FieldLimit(100)
	// info.AddField("区域", "acode", db.Text)
	info.AddField("内容栏目", "scode", db.Text).FieldFilterable(
		types.FilterType{Operator: types.FilterOperatorLike,
			FormType: form.SelectSingle,
			Options:  fopts}).FieldSortable().
		FieldEditAble(editType.Select).FieldEditOptions(fopts).
		FieldDisplay(func(value types.FieldModel) any {
			name, _ := value.Row["ay_content_sort_goadmin_join_name"].(string)
			return name
		})

	info.AddField("副栏目", "subscode", db.Text).FieldHide().FieldSortable()
	info.AddField("内容排序", "sorting", db.Integer).FieldHide()
	info.AddField("状态", "status", db.Text).FieldDisplay(filter01("隐藏", "显示")).
		FieldEditAble(editType.Switch).FieldEditOptions(opt01("隐藏", "显示"))
	info.AddField("是否置顶", "istop", db.Text).FieldDisplay(filter01("否", "是")).
		FieldEditAble(editType.Switch).FieldEditOptions(opt01("否", "是"))
	info.AddField("是否推荐", "isrecommend", db.Text).FieldDisplay(filter01("否", "是")).
		FieldEditAble(editType.Switch).FieldEditOptions(opt01("否", "是"))
	info.AddField("是否头条", "isheadline", db.Text).FieldDisplay(filter01("否", "是")).
		FieldEditAble(editType.Switch).FieldEditOptions(opt01("否", "是"))
	info.AddField("访问数", "visits", db.Integer)
	info.AddField("点赞数", "likes", db.Integer)
	info.AddField("反对数", "oppose", db.Integer)
	info.AddField("创建人员", "create_user", db.Text).FieldEditAble()
	// info.AddField("更新人员", "update_user", db.Text).FieldHide()
	// info.AddField("创建时间", "create_time", db.Text)
	info.AddField("更新时间", "update_time", db.Text)
	// info.AddField("Gtype", "gtype", db.Text).FieldEditAble()
	// info.AddField("Gid", "gid", db.Text).FieldHide()
	// info.AddField("Gnote", "gnote", db.Text).FieldHide()
	info.AddField("多图片标题", "picstitle", db.Text)
	info.AddActionButton(ctx, "baidu", action.Jump("https://www.baidu.com"))
	info.SetTable("ay_content").SetTitle("文章内容").SetDescription("文章内容")

	formList := ayContent.GetForm()
	formList.AddField("编号", "id", db.Integer, form.Default).
		FieldDisableWhenCreate().FieldDisableWhenUpdate()
	formList.AddField("标题", "title", db.Text, form.Text)
	formList.AddField("描述", "description", db.Text, form.Text)
	// formList.AddField("内容", "content", db.Text, form.RichText)
	formList.AddField("内容", "content", db.Text, form.Custom).
		// FieldCustomContent(cherryeditor.New().GetContent()).
		FieldCustomContent(cherryEditor).
		// FieldCustomContent(wangEditor).
		// FieldCustomContent(wangeditor.New().GetContent()).
		SetPreProcessFn(prefn("content"))

	formList.AddField("区域", "acode", db.Text, form.SelectSingle).
		FieldOptions(opt01("cn", "en")).FieldDefault("0")
	formList.AddField("内容栏目", "scode", db.Text, form.SelectSingle).
		FieldOptionsFromTable("ay_content_sort", "name", "scode").FieldDefault("0")
	formList.AddField("副栏目", "subscode", db.Text, form.SelectSingle).
		FieldOptionsFromTable("ay_content_sort", "name", "scode").FieldDefault("0")
	formList.AddField("标题颜色", "titlecolor", db.Text, form.Color).FieldDefault("#333333")
	formList.AddField("副标题", "subtitle", db.Text, form.Text)
	formList.AddField("自定义文件名", "filename", db.Text, form.Text)
	formList.AddField("作者", "author", db.Text, form.Text)
	formList.AddField("来源", "source", db.Text, form.Text)
	formList.AddField("外链地址", "outlink", db.Text, form.Text)
	formList.AddField("发布日期", "date", db.Text, form.Datetime).FieldNowWhenInsert()
	formList.AddField("缩略图", "ico", db.Text, form.Text)
	formList.AddField("多图片", "pics", db.Text, form.Text)
	formList.AddField("tag关键字", "tags", db.Text, form.Text)
	formList.AddField("附件", "enclosure", db.Text, form.Text)
	formList.AddField("关键字", "keywords", db.Text, form.Text)
	formList.AddField("内容排序", "sorting", db.Integer, form.Number).FieldDefault("255")
	formList.AddField("状态", "status", db.Text, form.Radio).
		FieldOptions(opt01("隐藏", "显示")).FieldDefault("1")
	formList.AddField("是否置顶", "istop", db.Text, form.Radio).
		FieldOptions(opt01("不置顶", "置顶")).FieldDefault("0")
	formList.AddField("是否推荐", "isrecommend", db.Text, form.Radio).
		FieldOptions(opt01("不推荐", "推荐")).FieldDefault("0")
	formList.AddField("是否头条", "isheadline", db.Text, form.Radio).
		FieldOptions(opt01("非头条", "头条")).FieldDefault("0")
	formList.AddField("访问数", "visits", db.Integer, form.Number).FieldDefault("0")
	formList.AddField("点赞数", "likes", db.Integer, form.Number).FieldDefault("0")
	formList.AddField("反对数", "oppose", db.Integer, form.Number).FieldDefault("0")
	formList.AddField("创建人员", "create_user", db.Text, form.Text).FieldDefault("admin")
	formList.AddField("更新人员", "update_user", db.Text, form.Text).FieldDefault("admin")
	formList.AddField("创建时间", "create_time", db.Text, form.Datetime).FieldNowWhenInsert()
	formList.AddField("更新时间", "update_time", db.Text, form.Datetime).
		FieldNowWhenUpdate()
	formList.AddField("Gtype", "gtype", db.Text, form.Text).FieldDefault("4")
	formList.AddField("Gid", "gid", db.Text, form.Text).FieldHide()
	formList.AddField("Gnote", "gnote", db.Text, form.Text).FieldHide()
	formList.AddField("多图片标题", "picstitle", db.Text, form.Text)

	ayContent.GetForm().SetTabGroups(types.NewTabGroups("id",
		"title", "description", "content", "scode", "subscode", "subtitle", "keywords",
		"tags", "enclosure", "status", "istop", "isrecommend", "isheadline",
		"author", "source", "picstitle").
		AddGroup("acode", "outlink", "subname", "ico", "pics", "date", "titlecolor",
			"filename", "sorting", "visits", "likes", "oppose", "gtype", "gid", "gnote",
			"create_user", "update_user", "create_time", "update_time")).
		SetTabHeaders("基本信息", "更多信息")

	formList.EnableAjax("成功", "失败")
	formList.SetTable("ay_content").SetTitle("编辑文章").SetDescription("编辑文章")
	return ayContent
}
